/**
 * Copyright 2007 Jason Horman, Pete Aykroyd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.functor;

import org.apache.commons.lang.ObjectUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/** A org.functor that takes two arguments and returns a <code>boolean</code> value. */
public abstract class BinaryPredicate<Param1Type,Param2Type> implements Serializable {
	public static BinaryPredicate TRUE = new True();
	public static BinaryPredicate FALSE = new False();

	public static BinaryPredicate isEqual = new IsEqual();
	public static BinaryPredicate isSame = new IsSame();
	public static BinaryPredicate lt = new LessThan();
	public static BinaryPredicate gt = new GreaterThan();


	/**
	 * Evaluate this predicate.
	 *
	 * @param left	the first element of the ordered pair of arguments
	 * @param right the second element of the ordered pair of arguments
	 * @return the result of this test for the given arguments
	 */
	public abstract boolean test(Param1Type left, Param2Type right);

	public static BinaryPredicate isEqual() {
		return isEqual;
	}

	public static BinaryPredicate isSame() {
		return isSame;
	}

	public static UnaryPredicate isEqual(final Object equalsThis) {
		return isEqual.bindRight(equalsThis);
	}

	public static UnaryPredicate isNotEqual(final Object equalsThis) {
		return isEqual.bindRight(equalsThis).not();
	}

	public static UnaryPredicate isSame(final Object sameAsThis) {
		return isSame.bindRight(sameAsThis);
	}

	public static UnaryPredicate isNotSame(final Object sameAsThis) {
		return isSame.bindRight(sameAsThis).not();
	}

	public static UnaryPredicate lt(Object lessThanThis) {
		return lt.bindRight(lessThanThis);
	}

	public static UnaryPredicate gt(Object greaterThanThis) {
		return gt.bindRight(greaterThanThis);
	}


	public UnaryPredicate<Param2Type> bindLeft(final Param1Type left) {
		return new LeftBinding(this, left);
	}

	public UnaryPredicate<Param1Type> bindRight(final Param2Type right) {
		return new RightBinding(this, right);
	}

	public static BinaryPredicate not(final BinaryPredicate predicate) {
		return new Not(predicate);
	}

	public BinaryPredicate not() {
		return not(this);
	}

	public BinaryPredicate and(final BinaryPredicate predicate) {
		return new And<Param1Type, Param2Type>(this, predicate);
	}

	public BinaryPredicate or(final BinaryPredicate predicate) {
		return new Or<Param1Type, Param2Type>(this, predicate);
	}

	/**
	 * And's a number of {@link UnaryPredicate unary predicates} together. Short
	 * circuits, meaning the first false stops the test.
	 */
	public static class And<Param1Type, Param2Type> extends BinaryPredicate<Param1Type, Param2Type> {
		public List<BinaryPredicate<Param1Type, Param2Type>> predicates;

		public And(BinaryPredicate<Param1Type, Param2Type> ... predicates) {
			this.predicates = Arrays.asList(predicates);
		}

		public boolean test(Param1Type left, Param2Type right) {
			for (BinaryPredicate<Param1Type, Param2Type> predicate : predicates) {
				if (!predicate.test(left, right))
					return false;
			}

			return true;
		}

		public String toString() {
			StringBuffer str = new StringBuffer();
			for (Iterator iterator = predicates.iterator(); iterator.hasNext();) {
				BinaryPredicate binaryPredicate = (BinaryPredicate) iterator.next();
				str.append(binaryPredicate);
				if (iterator.hasNext())
					str.append(" AND ");
			}

			return str.toString();
		}
	}

	/**
	 * And's a number of {@link UnaryPredicate unary predicates} together. Short
	 * circuits, meaning the first true stops the test.
	 */
	public static class Or<Param1Type, Param2Type> extends BinaryPredicate<Param1Type, Param2Type> {
		public List<BinaryPredicate<Param1Type, Param2Type>> predicates;

		public Or(BinaryPredicate<Param1Type, Param2Type> ... predicates) {
			this.predicates = Arrays.asList(predicates);
		}

		public boolean test(Param1Type left, Param2Type right) {
			for (BinaryPredicate<Param1Type, Param2Type> predicate : predicates) {
				if (predicate.test(left, right))
					return true;
			}

			return false;
		}

		public String toString() {
			StringBuffer str = new StringBuffer();
			for (Iterator iterator = predicates.iterator(); iterator.hasNext();) {
				BinaryPredicate binaryPredicate = (BinaryPredicate) iterator.next();
				str.append(binaryPredicate);
				if (iterator.hasNext())
					str.append(" OR ");
			}

			return str.toString();
		}
	}

	public class LeftBinding extends UnaryPredicate<Param2Type> {
		private final BinaryPredicate<Param1Type, Param2Type> predicate;
		private final Param1Type left;

		public LeftBinding(BinaryPredicate<Param1Type, Param2Type> predicate, Param1Type left) {
			this.predicate = predicate;
			this.left = left;
		}

		public boolean test(Param2Type obj) {
			return predicate.test(left, obj);
		}

		public String toString() {
			return predicate + "(" + left + ", ?)";
		}

		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;
			final LeftBinding that = (LeftBinding) o;
			if (!left.equals(that.left)) return false;
			if (!predicate.equals(that.predicate)) return false;
			return true;
		}

		public int hashCode() {
			int result;
			result = predicate.hashCode();
			result = 29 * result + left.hashCode();
			return result;
		}
	}

	public class RightBinding extends UnaryPredicate<Param1Type> {
		private final BinaryPredicate<Param1Type, Param2Type> predicate;
		private final Param2Type right;

		public RightBinding(BinaryPredicate<Param1Type, Param2Type> predicate, Param2Type right) {
			this.predicate = predicate;
			this.right = right;
		}

		public boolean test(Param1Type obj) {
			return predicate.test(obj, right);
		}

		public String toString() {
			return predicate + "(?, " + right + ")";
		}

		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;
			final RightBinding that = (RightBinding) o;
			if (!predicate.equals(that.predicate)) return false;
			if (!right.equals(that.right)) return false;
			return true;
		}

		public int hashCode() {
			int result;
			result = predicate.hashCode();
			result = 29 * result + right.hashCode();
			return result;
		}
	}

	public static BinaryPredicate le = new BinaryPredicate() {
		public boolean test(Object left, Object right) {
			return ((Comparable) left).compareTo(right) <= 0;
		}
	};

	public static class Not extends BinaryPredicate {
		private final BinaryPredicate predicate;

		public Not(BinaryPredicate predicate) {
			this.predicate = predicate;
		}

		public boolean test(Object left, Object right) {
			return !predicate.test(left, right);
		}

		public String toString() {
			return "not(" + predicate + ")";
		}
	}

	public static class IsEqual extends BinaryPredicate {
		public boolean test(Object left, Object right) {
			return ObjectUtils.equals(left, right);
		}

		public String toString() {
			return "isEqual";
		}

		public boolean equals(Object obj) {
			return obj instanceof IsEqual;
		}
	}

	public static class IsSame extends BinaryPredicate {
		public boolean test(Object left, Object right) {
			return left == right;
		}

		public String toString() {
			return "isSame";
		}

		public boolean equals(Object obj) {
			return obj instanceof IsSame;
		}
	}

	public static class True extends BinaryPredicate {
		public boolean test(Object left, Object right) {
			return true;
		}

		public BinaryPredicate or(BinaryPredicate predicate) {
			return this;
		}

		public BinaryPredicate not() {
			return FALSE;
		}

		public String toString() {
			return "true";
		}
	}

	public static class False extends BinaryPredicate {
		public boolean test(Object left, Object right) {
			return false;
		}

		public BinaryPredicate and(BinaryPredicate predicate) {
			return this;
		}

		public BinaryPredicate not() {
			return TRUE;
		}

		public String toString() {
			return "false";
		}
	}

	public static class LessThan<L extends Comparable, R extends Comparable> extends BinaryPredicate<L, R> {
		public boolean test(L left, R right) {
			return left.compareTo(right) < 0;
		}
	}

	public static class GreaterThan<L extends Comparable, R extends Comparable> extends BinaryPredicate<L, R> {
		public boolean test(L left, R right) {
			return left.compareTo(right) > 0;
		}
	}

}
